package com.zxp.mall.member.config;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//feign 远程调用丢失请求头
@Configuration
public class MallFeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {

        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
//                远程之前先走拦截器
                ServletRequestAttributes attributes =
                        (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if(attributes!=null) {
//                老请求
                    HttpServletRequest request = attributes.getRequest();
                    if (request != null) {
                        requestTemplate.header("Cookie", request.getHeader("Cookie"));
                    }
                }
            }
        };
    }
}
